import logging

import pytest

from resource.pom.music import Music


class Test:
    app = Music

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：点击音乐应用')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.stop_permission()
        logging.info('预期结果1.正常进入音乐应用内')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.点击播放按钮')
        device.refresh_layout()
        last_song = device.get_element_by_key('image2')
        play = device.get_element_by_key('image3')
        next_song = device.get_element_by_key('image4')
        device.click_element(play)
        device.wait(3)
        logging.info('预期结果2.音乐正常播放且有声音')
        device.refresh_layout()
        slider = device.get_element_by_type('Slider')
        assert slider.get('text') != 'nan', '音乐未正常播放'
        assert device.is_audio_running(), '音乐没有声音输出'

        logging.info('步骤3.点击上一首切换按钮')
        device.click_element(last_song)
        device.wait(2)
        logging.info('预期结果3.正常切换到上一首音乐并流畅播放')
        device.refresh_layout()
        device.assert_text_exist('demo.wav')
        slider = device.get_element_by_type('Slider')
        assert slider.get('text') != 'nan', '音乐未正常播放'
        assert device.is_audio_running(), '音乐没有声音输出'

        logging.info('步骤4.点击下一首切换按钮')
        device.click_element(next_song)
        device.wait(2)
        logging.info('预期结果4.正常切换到下一首音乐并流畅播放')
        device.refresh_layout()
        device.assert_text_exist('dynamic.wav')
        slider = device.get_element_by_type('Slider')
        assert slider.get('text') != 'nan', '音乐未正常播放'
        assert device.is_audio_running(), '音乐没有声音输出'
